package com.atguigu.daijia.driver.service.impl;

import com.atguigu.daijia.common.execption.GuiguException;
import com.atguigu.daijia.common.result.Result;
import com.atguigu.daijia.common.result.ResultCodeEnum;
import com.atguigu.daijia.dispatch.client.NewOrderFeignClient;
import com.atguigu.daijia.driver.service.OrderService;
import com.atguigu.daijia.map.client.MapFeignClient;
import com.atguigu.daijia.model.form.map.CalculateDrivingLineForm;
import com.atguigu.daijia.model.form.order.UpdateOrderCartForm;
import com.atguigu.daijia.model.vo.base.PageVo;
import com.atguigu.daijia.model.vo.map.DrivingLineVo;
import com.atguigu.daijia.model.vo.order.CurrentOrderInfoVo;
import com.atguigu.daijia.model.vo.order.NewOrderDataVo;
import com.atguigu.daijia.order.client.OrderInfoFeignClient;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class OrderServiceImpl implements OrderService {
    @Resource
    private MapFeignClient mapFeignClient;

    @Resource
    OrderInfoFeignClient orderInfoFeignClient;

    @Resource
    private NewOrderFeignClient newOrderFeignClient;

    /*
     * @param calculateDrivingLineForm 
     * @==============History===============<br/>;
     * @Description //   计算驾驶线路,这个是在根据乘客选择的出发地和目的地预估路程和时间
     * @Date 2024/8/9
     * @Author < a href="15991488967@163.com">chen</ a>;
     */
    @Override
    public DrivingLineVo calculateDrivingLine(CalculateDrivingLineForm calculateDrivingLineForm) {
        return mapFeignClient.calculateDrivingLine(calculateDrivingLineForm).getData();
    }

    /*
     * @param orderId
     * @==============History===============<br/>;
     * @Description //   根据订单id获取订单状态
     * @Date 2024/8/9
     * @Author < a href="15991488967@163.com">chen</ a>;
     */
    @Override
    public Integer getOrderStatus(Long orderId) {
        return orderInfoFeignClient.getOrderStatus(orderId).getData();
    }

    /*
     * @param driverId
     * @==============History===============<br/>;
     * @Description //   司机端查找当前订单
     * @Date 2024/8/9
     * @Author < a href="15991488967@163.com">chen</ a>;
     */
    @Override
    public CurrentOrderInfoVo searchDriverCurrentOrder(Long driverId) {
        return orderInfoFeignClient.searchDriverCurrentOrder(driverId).getData();
    }

    /*
     * @param driverId
     * @param page
     * @param limit
     * @==============History===============<br/>;
     * @Description //   查询司机订单列表
     * @Date 2024/8/9
     * @Author < a href="15991488967@163.com">chen</ a>;
     */
    @Override
    public PageVo findDriverOrderPage(Long driverId, Long page, Long limit) {
        return orderInfoFeignClient.findDriverOrderPage(driverId,page,limit).getData();
    }

    /*
     * @param driverId
     * @==============History===============<br/>
     * @Description //   查询司机新订单数据
     * @Date 2024-8-25 21:46 
     * @Author < a href="15991488967@163.com">chen</ a>
     * @return java.util.List<com.atguigu.daijia.model.vo.order.NewOrderDataVo>
     */
    @Override
    public List<NewOrderDataVo> findNewOrderQueueData(Long driverId) {
        return newOrderFeignClient.findNewOrderQueueData(driverId).getData();
    }


    /*
     * @param driverId    司机id
     * @param orderId     订单id
     * @==============History===============<br/>
     * @Description //   司机抢单
     * @Date 2024-9-10 22:23
     * @Author < a href="15991488967@163.com">chen</ a>
     * @return java.lang.Boolean
     */
    @Override
    public Boolean robNewOrder(Long driverId, Long orderId)
    {
        Result<Boolean> result = orderInfoFeignClient.robNewOrder(driverId, orderId);
        if(Objects.equals(result.getCode(), ResultCodeEnum.SUCCESS.getCode()))
        {
            return result.getData();
        }
        else
        {
            throw new GuiguException(ResultCodeEnum.FAIL);
        }
    }

    /*
     * @param orderId   订单id
     * @param driverId  司机id
     * @==============History===============<br/>
     * @Description //   司机到达代驾起始地点
     * @Date 2024-9-10 23:14
     * @Author < a href="15991488967@163.com">chen</ a>
     * @return java.lang.Boolean
     */
    @Override
    public Boolean driverArriveStartLocation(Long orderId, Long driverId)
    {
        Result<Boolean> result = orderInfoFeignClient.driverArriveStartLocation(orderId, driverId);

        if(Objects.equals(result.getCode(), ResultCodeEnum.SUCCESS.getCode()))
        {
            return result.getData();
        }
        throw new GuiguException(ResultCodeEnum.FAIL);
    }

    /*
     * @param updateOrderCartForm
     * @==============History===============<br/>
     * @Description //   更新代驾车辆信息
     * @Date 2024-9-10 23:20
     * @Author < a href="15991488967@163.com">chen</ a>
     * @return java.lang.Boolean
     */
    @Override
    public Boolean updateOrderCart(UpdateOrderCartForm updateOrderCartForm)
    {
        Result<Boolean> result = orderInfoFeignClient.updateOrderCart(updateOrderCartForm);
        if(Objects.equals(result.getCode(), ResultCodeEnum.SIGN_ERROR.getCode()))
        {
            return result.getData();
        }
        throw new GuiguException(ResultCodeEnum.FAIL);
    }
}
